package gui_classes;

import java_persistence.AppointmentMapper;
import java_persistence.PatientMapper;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;

import domain.*;

/*
 * Class that represents Doctor's home page
 */
public class DoctorPanel extends JPanel {
	private JComboBox comboBox;
	private JComboBox comboBox_1;
	private JButton btnViewPatient;
	private JButton btnAddPatient;
	private JButton btnViewIncomeStatistics;
	private JComboBox comboBox_2;
	private JLabel lblMonth;
	
//	private User doc;
	
	/**
	 * Create the panel.
	 */
	public DoctorPanel(User usr) {
		setLayout(null);
		setPreferredSize(new Dimension(600, 300));
	//	this.nurse = nur;
		GUI_Main.getFrame().setTitle("Logged in as " + usr.getName());
		
		ArrayList<String> appointments = new ArrayList<String>();

		List<Patient> patients = GUI_Main.getSession().getMapper(PatientMapper.class).get_patients(); // no such method.. why?
		int i=0;
		String[] patStr = new String[patients.size()];
		for( Iterator<Patient> patientsIter = patients.iterator(); patientsIter.hasNext(); ){
			Patient cur = patientsIter.next();
			List<String> apps = GUI_Main.getSession().getMapper(AppointmentMapper.class).get_appointment_dates(cur.getUsername());
			for(Iterator<String>  appsIter = apps.iterator(); appsIter.hasNext();){
				String current_app = cur.getUsername() + " " + appsIter.next();
				appointments.add(current_app);
			}
			patStr[i]=cur.getName() + " " + cur.getUsername();
			i++;
		}
		
		int j=0;
		String[] appStr = new String[appointments.size()];
		for(Iterator<String> appsIter = appointments.iterator(); appsIter.hasNext();){
			appStr[j] = appsIter.next();
			j++;
		}
	//	String[] patStr = (String[])patients.toArray();
	//	String[] patStr = {"James Washington", "Joseph Adams", "Dante Jefferson", "Pratik Monroe"};
		comboBox = new JComboBox(patStr);
		comboBox.setBounds(25, 65, 235, 24);
		add(comboBox);
		
		JLabel lblViewPatients = new JLabel("View Patients");
		lblViewPatients.setBounds(90, 38, 112, 15);
		add(lblViewPatients);
		
		btnAddPatient = new JButton("Add Patient");
		btnAddPatient.setBounds(70, 153, 132, 25);
		btnAddPatient.addActionListener(new AddPatientListener());
		add(btnAddPatient);
		
		btnViewPatient = new JButton("View Patient");
		btnViewPatient.setBounds(70, 116, 132, 25);
		btnViewPatient.addActionListener(new ViewPatientListener());
		add(btnViewPatient);
		
		btnViewIncomeStatistics = new JButton("View Income Statistics");
		btnViewIncomeStatistics.setBounds(55, 224, 193, 25);
		btnViewIncomeStatistics.addActionListener(new ViewIncomeStatsListener());
		add(btnViewIncomeStatistics);
		
		JComboBox comboBox_1 = new JComboBox(appStr);
		comboBox_1.setBounds(329, 65, 259, 24);
		add(comboBox_1);
		
		JLabel lblViewUpcomingAppointments = new JLabel("View Upcoming Appointments");
		lblViewUpcomingAppointments.setBounds(341, 38, 218, 15);
		add(lblViewUpcomingAppointments);
		
		String[] months = {"January", "February", "March", "April","May","June","July","August","September","October","November","December"};
		
		comboBox_2 = new JComboBox(months);
		comboBox_2.setBounds(260, 224, 179, 25);
		add(comboBox_2);
		
		lblMonth = new JLabel("Month");
		lblMonth.setBounds(304, 197, 70, 15);
		add(lblMonth);
		

	}
	
	/*
	 * returns the combobox of patients
	 * @return comboBox
	 */
	public JComboBox getComboBox()
	{
		return this.comboBox;
	}
	
	public JComboBox getComboBox1()
	{
		return this.comboBox_1;
	}
	
	/*
	 * getter for View Patient Button
	 */
	public JButton getVButton()
	{
		return this.btnViewPatient;
	}
	
	/*
	 * getter for Add Patient button
	 */
	public JButton getAButton()
	{
		return this.btnAddPatient;
	}
	
	/*
	 * Listener for View Patient Button
	 */
	private class ViewPatientListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			
			String[] patnames = ( (String) GUI_Main.dpage.getDPanel().getComboBox().getSelectedItem() ).split(" ");
			System.out.println("Doctor wants to view patient " + patnames[patnames.length-1]);
			Patient pat = GUI_Main.getSession().getMapper(PatientMapper.class).get_patient_by_username(patnames[patnames.length-1]);
			
			GUI_Main.viewpinfo = new ViewPatientPage(pat, GUI_Main.dpage);
			GUI_Main.viewpinfo.setPrevious(GUI_Main.dpage);
			GUI_Main.viewpinfo.display();
		}
	}
	
	/*
	 * Listener for Add patient listener
	 */
	private class AddPatientListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			System.out.println("Adding a patient");
			
			// add doctor inside the constructor
			GUI_Main.addpatientPage = new AddPatientPanel();
			GUI_Main.addpatientPage.setPrevious(GUI_Main.dpage);
			GUI_Main.addpatientPage.display();
			
		}
	}
	
	private class ViewIncomeStatsListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			System.out.println("Viewing income stats");
			
			Reports current = new Reports(GUI_Main.getSession());
			String month = (String) comboBox_2.getSelectedItem();
			String m = null;
			if(month.equals("January"))
				m = "01";
			if(month.equals("February"))
				m = "02";
			if(month.equals("March"))
				m = "03";
			if(month.equals("April"))
				m = "04";
			if(month.equals("May"))
				m = "05";
			if(month.equals("June"))
				m = "06";
			if(month.equals("July"))
				m = "07";
			if(month.equals("August"))
				m = "08";
			if(month.equals("September"))
				m = "09";
			if(month.equals("October"))
				m = "10";
			if(month.equals("November"))
				m = "11";
			if(month.equals("December"))
				m = "12";
			current.setMonth(m);
			String monthTotal = current.totalBilledForMonth();
			String totalPats = current.totalPatientsSeen();
			System.out.println(monthTotal);
			System.out.println(totalPats);
			GUI_Main.ichartpage = new IncomeChartPage(month, monthTotal, totalPats);
			GUI_Main.ichartpage.setPrevious(GUI_Main.dpage);
			GUI_Main.ichartpage.display();
			
		}
	}
	
	

}
